package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.NginxPurgeCacheUtil;
import cn.jdy.plugins.entity.ExamOptions;
import cn.jdy.plugins.service.ExamOptionsService;
import cn.jdy.plugins.service.ExamQuestionsService;


/**
 * @desc   测评试题选项控制操作类
 * @author nignqian
 * @date   2016-7-5
 */
@Controller
@RequestMapping("/examOptions")
public class ExamOptionsCtrl extends BaseCtrl {
	@Autowired
	private ExamOptionsService examOptionsService;
	
	@Autowired
	private ExamQuestionsService examQuestionsService;
	
	private static Long projectIds;
	private static Long liveIds;
	//默认列表路径
	private static String viewName = "redirect:/examOptions/list?questionId=&s=";

	/**
	 * 初始化
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(ExamOptions entity, String fromDate, String toDate,@RequestParam(required = false) Long liveId,@RequestParam(required = false) Long questionId,@RequestParam(required = false) Long projectId, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/evaluationManage/examOptionsList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
		
		session.setAttribute("questionId", questionId);
		projectIds=projectId;
		
				liveIds=liveId;
				mv.addObject("liveId", liveId);
		mv.addObject("projectId", projectId);
		mv.addObject("entity", entity);
		mv.addObject("fromDate", fromDate);
		mv.addObject("toDate", toDate);
		return mv;
	}
	
	
	
	
	/**
	 * 异步查询试题选项信息
	 * @param  examOptions
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(ExamOptions examOptions, String fromDate, String toDate) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", examOptionsService.query(examOptions, fromDate, toDate));
			jsonMap.put("iTotalDisplayRecords", examOptionsService.count(examOptions, fromDate, toDate));
			jsonMap.put("iTotalRecords", examOptionsService.count(examOptions));
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}	
	
	
	/**
	 * 修改选项的显示顺序
	 * @param  id,sort
	 * @return
	 */
	@RequestMapping("/updateSort")
	@ResponseBody
	public Map<String, Object> updateSort(@RequestParam("id") Long id,@RequestParam("sort") Integer sort) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String result = "0";  //0-成功 ,1-异常
		try {
			//获取要修改顺序的选项
			ExamOptions examOptions = this.examOptionsService.find(id);
			if(null != examOptions){
				examOptions.setSort(sort);
				this.examOptionsService.update(examOptions);
			}
			// 清理缓存
			NginxPurgeCacheUtil.purge(2, id);
		} catch(Exception e) {
			BaseLog.e(getClass(), "修改异常！", e);
			result = "1";
		}
		jsonMap.put("result", result);
		return jsonMap;
	}	
	

	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit() {
		return edit(null);
	}

	/**
	 * 编辑
	 * @param id
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable Long id) {
		ModelAndView mv = new ModelAndView("/evaluationManage/examOptionsDetail.jsp");
		mv.addObject("data", null== id || 0 == id ? id : this.examOptionsService.find(id));
		return mv;
	}

	/**
	 * 保存（更新）试题信息
	 * @param examOptions
	 * @return
	 */
	@RequestMapping("/save")
	public ModelAndView save(ExamOptions examOptions) {
		ModelAndView mv = new ModelAndView();
		try {
			if(null == examOptions.getId()) {
				//新增
				String newViewName = this.viewName.replaceFirst("=", "="+examOptions.getQuestionId());
				Integer maxSort = examOptionsService.getMaxSort(examOptions.getQuestionId());//获取当前最大显示顺序
				examOptions.setSort(maxSort+1);
				examOptions.setOptionScore(1);
				examOptions.setQuestionName(examQuestionsService.find(examOptions.getQuestionId()).getQuestionName());
				examOptions.setInsertTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.examOptionsService.save(examOptions);
				
				mv.setViewName(newViewName.concat("1"));
				mv.setViewName("redirect:/examOptions/list?liveId="+liveIds+"&projectId="+projectIds+"&questionId="+examOptions.getQuestionId());
			} else {
				//修改
				String newViewName = this.viewName.replaceFirst("=", "="+examOptions.getQuestionId());
				examOptions.setOptionScore(1);
				examOptions.setQuestionName(examQuestionsService.find(examOptions.getQuestionId()).getQuestionName());
				examOptions.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				
				this.examOptionsService.update(examOptions);
				
				mv.setViewName(newViewName.concat("2"));
				mv.setViewName("redirect:/examOptions/list?liveId="+liveIds+"&projectId="+projectIds+"&questionId="+examOptions.getQuestionId());

		    // 清理缓存
		    NginxPurgeCacheUtil.purge(2, examOptions.getId());
			}
		} catch(Exception e) {
			mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "保存异常！", e);
		}

		return mv;
	}
	
	
	/**
	 * 删除选项
	 * @return
	 */
	
	@RequestMapping("/del")
	@ResponseBody
	public  Map<String, Object> del(ExamOptions examOptions) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			this.examOptionsService.delete(examOptions.getId());
			String newViewName = this.viewName.replaceFirst("=", "="+examOptions.getQuestionId());
			jsonMap.put("msg", "成功");
			// 清理缓存
			NginxPurgeCacheUtil.purge(2, examOptions.getId());
		} catch(Exception e) {
			jsonMap.put("msg", "失败");
			BaseLog.e(getClass(), "删除异常！", e);
		}
		return jsonMap;
	}

}
